import Vue from 'vue';
//导入数据操作API
import api from "@/service/api";
import http from "@/service/http";

//状态
const state={
	groupId:'',
	//员工列表state
	employees:[], 
	//表格选择行数据
	selects:[],
	//总记录数
	total:0, 
	//当前页
	currentPage:1, 
	//每页显示记录数
	pageSize:10, 
	//表格加载状态
	listLoading:false, 
	//过滤条件
	filters:{
		employeename:'',
		employeesex:'',
		created_at:'',
		status:''
	},
	isToSelect:false,//是否来自选择用户
	ableTag:'1',//1:激活的员工
	selectItem:{//选中的员工
		userId:'',
		userName:'',
		realName:'',
		userType:''
	},
	isDialogShow:false
};

//getter
const getters={
	groupId:state=>state.groupId,
	employees:state=>state.employees,
	selects:state=>state.selects,
	total:state=>state.total,
	currentPage:state=>state.currentPage,
	pageSize:state=>state.pageSize,
	listLoading:state=>state.listLoading,
	filters:state=>state.filters,
	isToSelect:state=>state.isToSelect,
	selectItem:state=>state.selectItem,
	isDialogShow:state=>state.isDialogShow
};

//action
const actions={
	//查询员工列表
	getEmployees({commit,state}){
		let param={
			groupId:state.groupId,
			pageNum:state.currentPage,
			pageSize:state.pageSize,
			status:state.ableTag
		};
		commit('listLoading',true);
		http.post(api.sysApi.employeeList,param).then(data => {
			if(!data.success){
				return;
			}
            commit('loadList',data);
			commit('listLoading',false);
        }); 
	},
	//选择group
	selectGroup({commit,dispatch},groupId){
		commit('selectGroup',groupId);
		dispatch('getEmployees');
	},
	//表格选择行
	selectsChange({commit,dispatch},selects){
		commit('selectsChange',selects);
	},
	handleSelectItem({commit,dispatch},item){
		commit('selectItem',item);
	},
	//表格翻页
	currentChange({commit,dispatch},currentPage){
		commit('currentChange',currentPage);
		dispatch('getEmployees');
	},
	//表格设置每页条数
	sizeChange({commit,dispatch},pageSize){
		commit('sizeChange',pageSize);
		dispatch('getEmployees');
	},
	//重置过虑器
	resetFilters({commit,dispatch},filters){
		commit('resetFilters',filters);
		dispatch('getEmployees');
	},
	//表格载入状态
	listLoading({commit},isShow){
		commit('listLoading',isShow);
	},
	//是否来自选择用户
	toSelect({commit},isToSelect){
		commit('isSelectUser',isToSelect);
	},
	//是否显示为dialog
	showDialog({commit},isDialogShow){
		commit('setDialogShow',isDialogShow);
	},
	//选中单个员工
	selectEpmloyeeItem({commit},selectItem){
		commit('setSelectItem',selectItem);
	},
	//set status
	setStatus({commit},status){
		commit('setStatus',status);
	}
};

//mutations
const mutations={loadList(state,param){
		state.total=param.totalNum;
		state.employees=param.data;
	},
	selectGroup(state,groupId){
		state.groupId=groupId;
	},
	//表格载入状态变更
	listLoading(state,isShow){
		state.listLoading=isShow;
	},
	//表格选中行数据状态变更
	selectsChange(state,selects){
		state.selects=selects;
	},
	//表格翻页状态变更
	currentChange(state,currentPage){
		state.currentPage=currentPage;
	},
	selectItem(state,item){
		state.currentPage=currentPage;
	},
	//表格每页显示条数状态变更
	sizeChange(state,pageSize){
		state.pageSize=pageSize;
	},
	//查询条件过虑器状态充更
	resetFilters(state,filters){
		state.filters=filters;
	},
	//是否来自选择用户
	isSelectUser(state,isToSelect){
		state.isToSelect=isToSelect;
		state.ableTag = isToSelect?1:'';
	},
	setSelectItem(state,selectItem){   
		state.selectItem.userName=selectItem.userName;
		state.selectItem.realName=selectItem.realName;
	},
	setDialogShow(state,isDialogShow){
		state.isDialogShow=isDialogShow;
	},
	setStatus(state,status){
		state.ableTag = status;
	}
};

//导出
export default{
	//启用命名空间
	namespaced:true,
	state,
	getters,
	actions,
	mutations
};